﻿
using Abp.ObjectMapping;
using AVIS.OutBound.BookOrders.Dto;
using AVIS.OutBound.CarGroups;
using AVIS.OutBound.Cities;
using AVIS.OutBound.Countrys;
using AVIS.OutBound.Orders;
using AVIS.OutBound.RateCodes;
using AVIS.OutBound.Regions;
using AVIS.OutBound.Stores;
using AVIS.OutBound.Stores.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AVIS.OutBound.BookOrders
{
    /// <summary>
    /// 预订单的应用服务类
    /// </summary>
    public class BookOrderAppService : OutBoundAppServiceBase, IBookOrderAppService
    {
        #region 构造函数
        private readonly IBookOrderManager _bookOrderManager;
        private readonly IObjectMapper _objectMapper;
        private readonly IRateCodeManager _rateCodeMananger;
        private readonly IStoreManager _storeManager;
        private readonly ICityManager _cityManager;
        private readonly IRegionManager _regionManager;
        private readonly ICountryManager _countryManager;

        /// <summary>
        /// 构造函数
        /// </summary>
        public BookOrderAppService(
            IBookOrderManager bookOrderManager,
            IObjectMapper objectMapper,
            IRateCodeManager rateCodeMananger,
            IStoreManager storeManager,
            ICityManager cityManager,
            IRegionManager regionManager,
            ICountryManager countryManager)
        {
            _bookOrderManager = bookOrderManager;
            _objectMapper = objectMapper;
            _rateCodeMananger = rateCodeMananger;
            _storeManager = storeManager;
            _cityManager = cityManager;
            _regionManager = regionManager;
            _countryManager = countryManager;
        }
        #endregion

        /// <summary>
        /// 选择取还车门店预订接口
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public BookOrderIdOutput SubmitStore(SubmitStoreInput input)
        {
            // 根据门店预订信息生成一个预订单信息
            var bookOrder = BuildBookOrder(input);

            // 将预订单信息合并保存到缓存中（分几步来收集订单的信息）
            var BookOrderItem = _bookOrderManager.SaveBookOrderCache(bookOrder);
            return new BookOrderIdOutput { OrderId = BookOrderItem.OrderId };
        }

        /// <summary>
        /// 选择车组预订接口
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public BookOrderIdOutput SubmitCarGroup(SubmitCarGroupInput input)
        {
            var bookOrder = _objectMapper.Map<BookOrder>(input);

            // 将预订单信息合并保存到缓存中（分几步来收集订单的信息）
            var bookOrderItem = _bookOrderManager.SaveBookOrderCache(bookOrder);
            return new BookOrderIdOutput { OrderId = bookOrderItem.OrderId };
        }

        /// <summary>
        /// 选择增值服务预订接口
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public BookOrderIdOutput SubmitCarEquipment(SubmitCarEquipmentInput input)
        {
            var bookOrder = _objectMapper.Map<BookOrder>(input);

            // 将预订单信息合并保存到缓存中（分几步来收集订单的信息）,需要更新车组信息
            var BookOrderItem = _bookOrderManager.SaveBookOrderCache(bookOrder);
            return new BookOrderIdOutput { OrderId = BookOrderItem.OrderId };
        }

        /// <summary>
        /// 查询预订单接口
        /// </summary>
        /// <param name="orderId">订单ID</param>
        /// <returns></returns>
        public BookOrderOutput Detail(string orderId)
        {
            // 获取预订单信息
            var bookOrder = _bookOrderManager.GetBookOrder(orderId);
            var output = _objectMapper.Map<BookOrderOutput>(bookOrder);

            // 处理订单中的取还车门店信息
            var pickUpStore = _storeManager.GetStoreByCode(output.PickUpStoreCode);
            if (pickUpStore != null)
            {
                output.PickUpStore = _objectMapper.Map<StoreDto>(pickUpStore);
                output.PickUpStore.ShopOpenTime = pickUpStore.DayOpenTime(output.PickupDateTime);
            }
            var returnStore = _storeManager.GetStoreByCode(bookOrder.ReturnStoreCode);
            if (returnStore != null)
            {
                output.ReturnStore = _objectMapper.Map<StoreDto>(returnStore);
                output.ReturnStore.ShopOpenTime = returnStore.DayOpenTime(output.ReturnDateTime);
            }

            return output;
        }

        #region 私有方法
        /// <summary>
        /// 根据门店预订信息生成一个预订单信息
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        private BookOrder BuildBookOrder(SubmitStoreInput input)
        {
            var bookOrder = _objectMapper.Map<BookOrder>(input);

            // 设置默认值
            bookOrder.OrderType = "3";//订单类型
            bookOrder.PayType = "2";//付款类型
            bookOrder.OrderStep = 2;//订单状态
            bookOrder.RentalUnit = "天";

            var pickUpStore = _storeManager.GetStoreByCode(bookOrder.PickUpStoreCode);
            bookOrder.PickUpStoreName = pickUpStore?.CNName;

            var returnStore = _storeManager.GetStoreByCode(bookOrder.ReturnStoreCode);
            bookOrder.ReturnStoreName = returnStore?.CNName;

            var pickUpCity = _cityManager.GetCity(bookOrder.PickUpCityCode, bookOrder.PickUpRegionCode);
            bookOrder.PickUpCityName = pickUpCity?.CNName;

            var returnCity = _cityManager.GetCity(bookOrder.ReturnCityCode, bookOrder.ReturnRegionCode);
            bookOrder.ReturnCityName = returnCity?.CNName;

            var region = _regionManager.GetRegion(bookOrder.PickUpRegionCode);
            if (region != null)
            {
                var country = _countryManager.GetCountry(region.CountryCode, region.CountryName);
                bookOrder.CountryName = country?.ENName;
                bookOrder.CountryCode = country?.Code;
            }

            return bookOrder;
        }
        #endregion
    }
}
